<!DOCTYPE html>
<HTML>
<HEAD><meta name="viewport" content="width=device-width, initial-scale=1">
<Title>Percentage Bar Chart</Title>
<link type='text/css' rel='Stylesheet' href="maxchartapi.css" />
</HEAD>
<body bgcolor="#FFFFFF" text="#000000" topmargin="0" leftmargin="0" rightmargin="0" marginwidth="0" marginheight="0">

<p class="heading0">ChartDirector 7.0 (Java Edition)</p>
<p class="heading1">Percentage Bar Chart</p>
<hr class="separator">
<div class="content">
<img src="images/percentbar.png" width='510' height='330'>
<br><br>
This example demonstrates creating a percentage bar chart with a legend box. It also demonstrates gradient background color, rounded frame, soft drop shadow, and using <a href="cdml.htm">CDML</a> to include custom icons in the legend box.<br><br>
A percentage bar chart is like a stacked bar chart, except the bars are individually scaled so that they stacked up to 100.<br><br>
The key features demonstrated in this example are:<br><br>
<ul><li> The gradient background is achieved by using <a href="BaseChart.linearGradientColor.htm">BaseChart.linearGradientColor</a> to define the gradient color, then use <a href="BaseChart.setBackground.htm">BaseChart.setBackground</a> to set it as the chart background color.<br><br>
<li> The rounded frame is configured using <a href="BaseChart.setRoundedFrame.htm">BaseChart.setRoundedFrame</a>.<br><br>
<li> The soft drop shadow is configured using <a href="BaseChart.setDropShadow.htm">BaseChart.setDropShadow</a>.<br><br>
<li> The legend box is added using <a href="BaseChart.addLegend.htm">BaseChart.addLegend</a>. The legend key (the color box to the right of each legend entry) is set to 16 x 32 pixels using <a href="LegendBox.setKeySize.htm">LegendBox.setKeySize</a>, so as to match the size of the custom icons.<br><br>
<li> The ordering of the legend entries is reversed using <a href="Layer.setLegend.htm">Layer.setLegend</a>, so that the legend box shows the last data set name first. This is useful for a vertical legend box in a vertical stacked chart with purely positive data. In such case, the last data set will be on the top of the stacked bars. Reversing the ordering of the legend entries will make it visually consistent with the stacking order of the data set.<br><br>
<li> The percentage bar layer is added using <a href="XYChart.addBarLayer2.htm">XYChart.addBarLayer2</a> with the <a href="Chart.Percentage.htm">Percentage</a> predefined constant.<br><br>
<li> Multiple data sets are added to the bar layer using <a href="Layer.addDataSet.htm">Layer.addDataSet</a>. <a href="cdml.htm">CDML</a> is used to include custom icons in the data set names.<br><br>
<li> Labels for the bar segments are added using <a href="Layer.setDataLabelStyle.htm">Layer.setDataLabelStyle</a>, with center alignment configured using <a href="TextBox.setAlignment.htm">TextBox.setAlignment</a>.</ul>
Note that by default, the data label format is <b>{percent}%</b> (showing the percentage) for a percentage bar layer, as opposed to <b>{value}</b> (showing the data value) for other types of bar layer. The data label format can be modified using <a href="Layer.setDataLabelFormat.htm">Layer.setDataLabelFormat</a>.
</div>
<p class="heading1a">Source Code Listing</p>
<div class="content">
<b>[JSP Version]</b> jspdemo/percentbar.jsp
<div class='codeblock'><code class='pre'>&lt;%@page import="ChartDirector.*, java.util.*" %&gt;
&lt;%
// The data for the bar chart
double[] data0 = {100, 125, 245, 147, 67};
double[] data1 = {85, 156, 179, 211, 123};
double[] data2 = {97, 87, 56, 267, 157};

// The labels for the bar chart
String[] labels = {"Mon", "Tue", "Wed", "Thu", "Fri"};

// Create a XYChart object of size 500 x 320 pixels. Use a vertical gradient color from pale blue
// (e8f0f8) to sky blue (aaccff) spanning half the chart height as background. Set border to blue
// (88aaee). Use rounded corners. Enable soft drop shadow.
XYChart c = new XYChart(500, 320);
c.setBackground(c.linearGradientColor(0, 0, 0, c.getHeight() / 2, 0xe8f0f8, 0xaaccff), 0x88aaee);
c.setRoundedFrame();
c.setDropShadow();

//Set search path to current JSP directory for loading icon images
c.setSearchPath(getServletConfig().getServletContext(), request);

// Add a title to the chart using 15 points Arial Italic. Set top/bottom margins to 15 pixels.
TextBox title = c.addTitle("Weekly Product Sales", "Arial Italic", 15);
title.setMargin2(0, 0, 15, 15);

// Tentatively set the plotarea to 50 pixels from the left edge, and to just under the title. Set
// the width to 60% of the chart width, and the height to 50 pixels from the bottom edge. Use pale
// blue (e8f0f8) background, transparent border, and grey (aaaaaa) grid lines.
c.setPlotArea(50, title.getHeight(), c.getWidth() * 6 / 10, c.getHeight() - title.getHeight() - 50,
    0xe8f0f8, -1, Chart.Transparent, 0xaaaaaa);

// Add a legend box where the top-right corner is anchored at 10 pixels from the right edge, and
// just under the title. Use vertical layout and 8 points Arial font.
LegendBox legendBox = c.addLegend(c.getWidth() - 10, title.getHeight(), true, "Arial", 8);
legendBox.setAlignment(Chart.TopRight);

// Set the legend box background and border to transparent
legendBox.setBackground(Chart.Transparent, Chart.Transparent);

// Set the legend box icon size to 16 x 32 pixels to match with custom icon size
legendBox.setKeySize(16, 32);

// Set axes to transparent
c.xAxis().setColors(Chart.Transparent);
c.yAxis().setColors(Chart.Transparent);

// Set the labels on the x axis
c.xAxis().setLabels(labels);

// Add a percentage bar layer
BarLayer layer = c.addBarLayer2(Chart.Percentage);

// Add the three data sets to the bar layer, using icons images with labels as data set names
layer.addDataSet(data0, 0x66aaee, "&lt;*block,valign=absmiddle*&gt;&lt;*img=service.png*&gt; Service&lt;*/*&gt;");
layer.addDataSet(data1, 0xeebb22, "&lt;*block,valign=absmiddle*&gt;&lt;*img=software.png*&gt; Software&lt;*/*&gt;");
layer.addDataSet(data2, 0xcc88ff, "&lt;*block,valign=absmiddle*&gt;&lt;*img=computer.png*&gt; Hardware&lt;*/*&gt;");

// Use soft lighting effect with light direction from top
layer.setBorderColor(Chart.Transparent, Chart.softLighting(Chart.Top));

// Enable data label at the middle of the the bar
layer.setDataLabelStyle().setAlignment(Chart.Center);

// For a vertical stacked chart with positive data only, the last data set is always on top.
// However, in a vertical legend box, the last data set is at the bottom. This can be reversed by
// using the setLegend method.
layer.setLegend(Chart.ReverseLegend);

// Adjust the plot area size, such that the bounding box (inclusive of axes) is 15 pixels from the
// left edge, just below the title, 10 pixels to the right of the legend box, and 15 pixels from the
// bottom edge.
c.packPlotArea(15, title.getHeight(), c.layoutLegend().getLeftX() - 10, c.getHeight() - 15);

// Output the chart
WebChartViewer viewer = new WebChartViewer(request, "chart1");
viewer.setChart(c, Chart.SVG);

// Include tool tip for the chart
viewer.setImageMap(c.getHTMLImageMap("", "",
    "title='{dataSetName} revenue on {xLabel}: US${value}K ({percent}%)'"));
%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Percentage Bar Chart&lt;/title&gt;
    &lt;!-- Include ChartDirector Javascript Library to support chart interactions --&gt;
    &lt;script type="text/javascript" src="cdjcv.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body style="margin:5px 0px 0px 5px"&gt;
    &lt;div style="font:bold 18pt verdana;"&gt;
        Percentage Bar Chart
    &lt;/div&gt;
    &lt;hr style="border:solid 1px #000080; background:#000080" /&gt;
    &lt;div style="font:10pt verdana; margin-bottom:1.5em"&gt;
        &lt;a href="viewsource.jsp?file=&lt;%=request.getServletPath()%&gt;"&gt;View Source Code&lt;/a&gt;
    &lt;/div&gt;
    &lt;!-- ****** Here is the chart image ****** --&gt;
    &lt;%= viewer.renderHTML(response) %&gt;
&lt;/body&gt;
&lt;/html&gt;</code></div>
</div>
<br><hr class="separator"><div class="copyright">&copy; 2022 Advanced Software Engineering Limited. All rights reserved.</div>
</body>
</HTML>
